package com.youhong.teethcare.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.youhong.teethcare.R;
import com.youhong.teethcare.services.Common;

import java.io.IOException;

/**
 * Created by Administrator on 2018/5/18 0018.
 */

public class LowBatteryDialog extends Dialog implements View.OnClickListener {
    public LowBatteryDialog(@NonNull Context context) {
        this(context, R.style.dialog_choose);
    }

    Button btn_ok;
    MediaPlayer mMediaPlayer;

    public LowBatteryDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        //dialog根布局为LinearLayout且orientation为vertical时，dialog最大宽高为布局里面控件的宽高
        //RelativeLayout则会是match_parent效果

        setContentView(R.layout.dialog_low_battery);
        btn_ok = findViewById(R.id.lowBattery_btn_ok);
        btn_ok.setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMediaPlayer = MediaPlayer.create(getContext(), getDefaultAlarm());
        mMediaPlayer.setLooping(true);//设置循环
        mMediaPlayer.start();
        Common.isLowBatteryNotificationPlayed = true;

    }


    private Uri getDefaultAlarm() {

        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        return uri;
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        mMediaPlayer.stop();
    }

    @Override
    public void onClick(View v) {

        if (v == btn_ok) {
            this.dismiss();
        }
    }
}
